import guinator
from twisted.internet import reactor
from twisted.protocols import basic
from twisted.internet.protocol import ClientFactory


class ChatClient(basic.NetstringReceiver):
    history = guinator.Attribute()
    
    def connectionMade(self):
        self.history = []
        self.sendString(self.factory.username)
    
    def stringReceived(self, text):
        self.history.append(text)
    
    @guinator.action(text = str)
    def say(self, text):
        self.sendString(text)


class ChatClientFactory(ClientFactory):
    protocol = ChatClient
    
    def __init__(self, username):
        self.username = username
    
    @guinator.action(host = str, port = int, username = str)
    @classmethod
    def connect(cls, host, port, username):
        inst = cls(username)
        reactor.connectTCP(host, port, inst)

guinator.main(ChatClientFactory.connect)














